package com.ysd.Controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.Service.ModulesServcie;
import com.ysd.Service.RolesService;
import com.ysd.entity.Department;
import com.ysd.entity.Modules;
import com.ysd.entity.roles;
import com.ysd.util.ResultInfoUtil;
import com.ysd.util.Treemodule;

@RestController
@RequestMapping("/roles")
@CrossOrigin
public class RolesController {
	@Autowired
	private RolesService rolesService;
	@Autowired
	private ModulesServcie modulesService;
	@GetMapping("/findAll")
	public ResultInfoUtil findAll(Integer page,Integer limit,roles r) {
		Pageable pageable =PageRequest.of(page-1, limit);
		Page<roles> roPage =rolesService.findAllRoles(r, pageable);
		return new ResultInfoUtil().success(roPage.getContent(),roPage.getTotalElements());
	}
	/**
	 * 删除
	 * @param uid
	 * @return
	 */
	@DeleteMapping("/deleteRoles")
	public String deleteRoles(String rid) {
		roles roles=new roles();
		roles.setRid(rid);
		try {
			rolesService.deleteRoles(roles);
			return "删除成功";
		} catch (Exception e) {
			// TODO: handle exception
			return "删除失败";
		}
	}
	@PostMapping("/addRoles")
	public roles addRoles(roles r) {
		UUID.randomUUID().toString();
		r.setRid(UUID.randomUUID().toString());
		return rolesService.addAndUpdate(r);
	}
	@PutMapping("/updateRoles")
	public roles updateRoles(roles r) { 
		return rolesService.addAndUpdate(r);
	}
	
	
	/**
	 * 根据角色id得到所属模块 ：角色模块
	 * 
	 * @param roleid
	 * @return
	 */
	@GetMapping("/ByidfindModules")
	public  List<Treemodule> ByidfindModules(String rid){
		//List<Modules>
		System.out.println("roleid=="+rid);
	  Optional<roles> r=rolesService.ByidfindModules(rid);
	  System.out.println("r"+r.get());
	  List<Modules>	mlist=r.get().getModules();
	  System.out.println(mlist.size());
	  List<Treemodule> newmodules=modulesService.getModulesTree(mlist);
	  System.out.println("newmodules"+newmodules.size());
	  
	  List<Treemodule>  t=new ArrayList<Treemodule>();
	  
	  for (int i = 0; i < newmodules.size(); i++) {
		  System.out.println(newmodules.get(i).getChildren().size());
		  if(newmodules.get(i).getChildren().size()>0)
			 for (int j = 0; j < newmodules.get(i).getChildren().size(); j++) {
				 t.add(newmodules.get(i).getChildren().get(j));
			}
			 
		  }
	  return t;
	}
	
	
	/**
	 *  角色模块设置
	 * @param roleid
	 * @param parentIds
	 * @return
	 */
	@RequestMapping("/SetSysRights")
	public Object SetSysRights(String Id,String parentIds) {
		System.out.println("我进入了角色模块权限设置");
		System.out.println( "parentIds"+parentIds);
		Map<String, Object> map= new HashMap<String, Object>();
		rolesService.delModulesrole(Id);
		if(parentIds!=null) {
			System.out.println("parentIds"+parentIds);
			try {
				rolesService.delModulesrole(Id);
			} catch (Exception e) {
				// TODO: handle exception
			}
			for(String id:parentIds.split(",")) {
				int mid = Integer.valueOf(id);
				rolesService.saveModulesrole(Id, mid);
			}
			map.put("success", true);
		}else {
			System.out.println("未设置权限");
			map.put("success", false);
		}
		return map;
		
	}
}
